//
//  WRStackLayout.m
//  01-UICollectionView显示图片
//
//  Created by 紫荆秋雪 on 16/3/5.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

#import "WRStackLayout.h"

@implementation WRStackLayout

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{

    return YES;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{

    //1、自定义一个可变数组
//    NSMutableArray *arrayM = [NSMutableArray array];
//    
//    //2、获取第0组Item的个数
//    NSInteger count = [self.collectionView numberOfItemsInSection:0];
//    
//    //3、通过遍历来来给0组的每一个Item设置属性
//    for (int i = 0; i < count; i++) {
    
        //3.1获取第0组中每个Item的属性
        UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        //3.2 设置Item的尺寸
        attr.size = CGSizeMake(100, 100);
        attr.center = CGPointMake(arc4random_uniform(self.collectionView.frame.size.width), arc4random_uniform(self.collectionView.frame.size.height));
        
        //3.3 设置Item的缩放
//        attr.transform3D = CATransform3DMakeRotation(M_2_PI, 1.0, 1.0, 1.0);
    
//        [arrayM addObject:attr];
//    }

    return attr;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{

    //1、自定义一个可变数组
    NSMutableArray *arrayM = [NSMutableArray array];
    
    //2、获取第0组Item的个数
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    
    //定义旋转角度
    NSArray *angles = @[@0, @0.2, @0.5, @(-0.5), @(-0.2)];
    
    //3、通过遍历来来给0组的每一个Item设置属性
    for (int i = 0; i < count; i++) {
        
        //3.1获取第0组中每个Item的属性
        UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        
        //3.2 设置Item的尺寸
        CGFloat centerX = self.collectionView.frame.size.width * 0.5;
        CGFloat centerY = self.collectionView.frame.size.height * 0.5;
        attr.size = CGSizeMake(150, 150);
        attr.center = CGPointMake(centerX, centerY);
        if (i >= 5) {
            attr.hidden = YES;
        } else {
            //3.3 设置Item的缩放
            attr.transform = CGAffineTransformMakeRotation([angles[i] floatValue]);
            attr.zIndex = count - i;
        }
        
        [arrayM addObject:attr];
    }

    
    return arrayM;
}


@end
